package bma.common.langutil.core;

/**
 * 
 * @author guanzhong
 * @since 1.0 (imported from BMA.ESP Project)
 * 
 * @param <TYPE>
 */
public class LinkItem<TYPE> {

	private LinkItem<TYPE> next;
	private LinkItem<TYPE> prev;
	private TYPE item;

	public LinkItem() {
		super();
	}

	public LinkItem(LinkItem<TYPE> next) {
		super();
		this.next = next;
	}

	public LinkItem<TYPE> getNext() {
		return next;
	}

	public void setNext(LinkItem<TYPE> next) {
		this.next = next;
	}

	public LinkItem<TYPE> getPrev() {
		return prev;
	}

	public void setPrev(LinkItem<TYPE> prev) {
		this.prev = prev;
	}

	public TYPE getItem() {
		return item;
	}

	public void setItem(TYPE item) {
		this.item = item;
	}

	public boolean isTail() {
		return this.next == null;
	}

	public boolean isHead() {
		return this.prev == null;
	}

	public TYPE get() {
		return item;
	}

	public void remove() {
		if (this.prev != null) {
			this.prev.next = this.next;
		}
		if (this.next != null) {
			this.next.prev = this.prev;
		}
	}

	public static <TYPE> LinkItem<TYPE> addTail(LinkItem<TYPE> head,
			LinkItem<TYPE> item) {
		if (head == null)
			return item;
		LinkItem<TYPE> h = head;
		while (h.getNext() != null) {
			h = h.getNext();
		}
		h.setNext(item);
		return head;
	}

	public static <TYPE> LinkItem<TYPE> addTail(LinkItem<TYPE> head, TYPE item) {
		LinkItem<TYPE> li = new LinkItem<TYPE>();
		li.setItem(item);
		return addTail(head, li);
	}
}
